# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     run_tests.py
    Description:   运行主入口: 调用各模块接口，执行测试
 -------------------------------------------------
 """
import logging

from apitesting.common.util.logger import log
from apitesting.src.framework_initializer import FrameworkInitializer
from apitesting.src.report_manager import ReportManager
from apitesting.src.run_engine import start_run, setup_check_run_args

# Configure logging
logging.basicConfig(
    level=logging.NOTSET,
    format='%(message)s'  # 只显示消息内容
)


banner = f"""
     ██     ███████  ██      ██████████ ████████  ████████ ██████████
    ████   ░██░░░░██░██     ░░░░░██░░░ ░██░░░░░  ██░░░░░░ ░░░░░██░░░ 
   ██░░██  ░██   ░██░██         ░██    ░██      ░██           ░██    
  ██  ░░██ ░███████ ░██         ░██    ░███████ ░█████████    ░██    
 ██████████░██░░░░  ░██         ░██    ░██░░░░  ░░░░░░░░██    ░██    
░██░░░░░░██░██      ░██         ░██    ░██             ░██    ░██    
░██     ░██░██      ░██         ░██    ░████████ ████████     ░██    
░░      ░░ ░░       ░░          ░░     ░░░░░░░░ ░░░░░░░░      ░░   

Starting...
"""


def main():
    """测试框架运行主入口"""

    logging.info(banner)

    try:
        # 初始化框架
        FrameworkInitializer.initialize_framework(pydantic_verify=True)
        log.info("✅ 测试框架初始化完成")
    except RuntimeError:
        log.error("❌ 测试框架初始化失败，终止运行")
        return

    try:
        # 设置运行参数
        run_args = setup_check_run_args()
        log.info(f"✅ 运行参数设置成功: {run_args}")
    except Exception as e:
        log.error(f"❌ 运行参数设置失败: {e}")
        raise e

    try:
        # 执行测试
        start_run(run_args)
        log.info("🏁 测试用例执行完成")
    except Exception as e:
        log.error(f"❌ 测试用例执行失败: {e}")

    # 调用报告模块
    try:
        ReportManager.generate_report()
        ReportManager.start_allure_server()
    except RuntimeError:
        log.error("❌ 报告处理失败，检查生成和服务启动流程")


if __name__ == "__main__":
    main()
